package com.dms.controller;

import com.dms.pojo.Dormitory;
import com.dms.service.DormitoryManageService;
import com.dms.utils.Msg;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;


/**
 * @className: DormitoryManageController
 * @package: com.dms.controller
 * @description  宿舍管理模块控制层
 * @author: sk
 */
@Controller
@RequestMapping("/dormitoryManageController")
public class DormitoryManageController {

    @Autowired
    DormitoryManageService dormitoryManageService;

    /**
     * 判断该楼栋该宿舍号是否存在  存在返回fail，不存在返回success
     * @param buildingId
     * @param dormitoryRoom
     * @return
     */
    @GetMapping("/checkRoomByBuildingNumber")
    @ResponseBody
    public Msg checkRoomByBuildingNumber(@RequestParam("buildingId") int buildingId, @RequestParam("dormitoryRoom") String dormitoryRoom){
        return dormitoryManageService.checkRoomByBuildingNumber(buildingId, dormitoryRoom) ? Msg.success() : Msg.fail();
    }

    /**
     *  录入寝室
     * @param dormitory
     * @return
     */
    @PostMapping("/addDormitory")
    @ResponseBody
    public Msg addDormitory(@RequestBody Dormitory dormitory){
        return dormitoryManageService.addDormitory(dormitory) > 0 ? Msg.success() : Msg.fail();
    }

    /**
     *  模糊查询  获取楼栋寝室列表,  获取寝室所住学生列表信息
     * @param query
     * @param select
     * @param pageNum
     * @param pageSize
     * @return
     */
    @GetMapping("/getDormitoryListLimit")
    @ResponseBody
    public Msg getDormitoryListLimit(@RequestParam(value = "query",defaultValue = "") String query, @RequestParam(value = "select",defaultValue = "0") int select, @RequestParam("pageNum") int pageNum,
                                     @RequestParam("pageSize") int pageSize){
        return Msg.success().add("dormitoryList", dormitoryManageService.getDormitoryListLimit(query, select,pageNum,pageSize))
                .add("total", dormitoryManageService.getDormitoryTotal(query, select));
    }


    /**
     * 获取寝室信息
     * @param buildingId
     * @param dormitoryRoom
     * @return
     */
    @GetMapping("/getDormitoryInfo")
    @ResponseBody
    public Msg getDormitoryInfo(@RequestParam("buildingId") int buildingId, @RequestParam("dormitoryRoom") String dormitoryRoom){
        return Msg.success().add("dormitoryInfo", dormitoryManageService.getDormitoryInfo(buildingId, dormitoryRoom));
    }

    /**
     *  修改寝室信息
     * @param dormitory
     * @return
     */
    @PutMapping("/editDormitory")
    @ResponseBody
    public Msg editDormitory(@RequestBody Dormitory dormitory){
        try {
            int num = dormitoryManageService.editDormitory(dormitory);
            if (num > 0)
                return Msg.success();
            return Msg.fail();
        }catch (Exception e){
            return Msg.exception();
        }
    }

    /**
     * 删除该寝室，并更新原寝室人员的dormitoryId为null
     * @param dormitoryId
     * @return
     */
    @DeleteMapping("/deleteDormitory")
    @ResponseBody
    public Msg deleteDormitory(@RequestParam("dormitoryId") int dormitoryId){
        int num = dormitoryManageService.batchUpdateStudentDormitoryId(dormitoryId);
        if (num > 1)
            return Msg.success();
        else
            return Msg.fail();
    }

}
